(function($) {
	var OrgTree = function() {
		var onClick = null;
		var setting = {
			data : {
				simpleData : {
					enable : true
				}
			},
			async: {
				enable: true,
				url: contextPath + "/tree/getUserNodes.json",
				autoParam: ["id=orgId"],
				type : "post",
				dataType: "json",
				dataFilter:function(treeId, parentNode, data){
					var treeNode = null;
					if (data && data.length > 0) {
						for (var i = 0; i < data.length; i++) {
							data[i].isParent = data[i].parent;
						}
					}
					treeNode = data;
					return treeNode;
				}
			},
			callback : {
				onClick : zTreeOnClick,
				onNodeCreated: zTreeOnNodeCreated
				//beforeExpand : beforeExpand
			}
		};
		
		this.createTree = function(treeId, url, data,clickFunc) {
			onClick = clickFunc;
			$.ajax({
				cache : false,
				async : true,
				url: url,
				type: "post",
				data: data,
				dataType: "json",
				success:function(data){
					if(data && data.length > 0){
						for(var i=0;i<data.length;i++){
							data[i].isParent = data[i].parent;
						}
					}
					$.fn.zTree.init($("#"+treeId), setting, data);
			    }
			});
		}
		
		function zTreeOnNodeCreated(event, treeId, treeNode) {
			var zTree = $.fn.zTree.getZTreeObj(treeId);
			var nodes = zTree.getNodes();
			if (nodes.length>0) {
				zTree.expandNode(nodes[0],true,false,false);
			}
		}
		
		function zTreeOnClick(event, treeId, treeNode, clickFlag) {
			if(onClick){
				onClick(treeNode)
			}
		}
	};
	$.orgTree = new OrgTree();
})(jQuery)